package com.example.myapp.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import com.example.myapp.bill.Bill;

import java.util.List;

public class BillDao {
    private final String tableName = "bills";

    private DatabaseAdapter databaseAdapter;

    private SQLiteDatabase database;

    private Context context;

    public BillDao(Context context) {
        databaseAdapter = DatabaseAdapter.getInstance();
        database = databaseAdapter.open(context);
        this.context = context;
    }

    public BillDao(DBHelper dbHelper) {
        databaseAdapter = DatabaseAdapter.getInstance();
        database = databaseAdapter.open(dbHelper);
    }

    public List<Bill> getAllListaBills() {
        Cursor cursor = database.query(tableName, null, null, null, null, null, null);
        List<Bill> bills = BillsUtilities.getAllListaRachunkowFromCursorObiect(cursor);
        cursor.close();
        return bills;
    }

    public Bill save(Bill bill) {
        ContentValues newTaskValues = BillContentValuesBuilder.createContentValuesByBill(bill);
        long res = database.insert(tableName, null, newTaskValues);
        validDBOperation(res);
        bill.setId(res);
        return bill;
    }

    public int delete(Bill bill) {
        String where = "id_rachunku=" + bill.getId();
        int res = database.delete(tableName, where, null);
        validDBOperation(res);
        return res;
    }

    public int paidBill(Bill bill) {
        bill.setIfPaid(true);
        String where = "id_rachunku=" + bill.getId();
        ContentValues newBillValues = BillContentValuesBuilder.createContentValuesByBill(bill);
        int res = database.update(tableName, newBillValues, where, null);
        validDBOperation(res);
        return res;
    }


    private void validDBOperation(long res) {
        if (res == -1) {
            try {
                throw new OperationFailedException();
            } catch (OperationFailedException e) {
                Toast.makeText(context, "OperationFailedException", Toast.LENGTH_LONG).show();
            }
        }
        if (res == 0) {
            try {
                throw new RecordNotFoundException();
            } catch (RecordNotFoundException e) {
                Toast.makeText(context, "RecordNotFoundException", Toast.LENGTH_LONG).show();
            }
        }
    }

    public List<Bill> getPaidBillsForMonth(String date) {
        Cursor cursor = database.query(tableName, null, null, null, null, null, null);
        List<Bill> bills = BillsUtilities.getAllListaRachunkowFromCursorObiectForMonth(cursor, date);
        cursor.close();
        return bills;
    }

    private class OperationFailedException extends Throwable {
    	private static final long serialVersionUID = 39283l;
    }

    private class RecordNotFoundException extends Throwable {
    	private static final long serialVersionUID = 3928322l;
    }
}
